import { MapData, type FeatureDataInterface, type FeatureInterface, type MapDataInterface } from "@/class/map/MapData";
import { convert_gcj02_To_Gps84, convert_gps84_To_Gcj02, MapCoordSys } from "@/class/map/myMapOpt";
import { reqDeleteMapData } from "@/api/mapdata";
import { reqPostTrack, reqPutTrack } from "@/api/track"
import { requestIsSuccess } from "@/api/request";
import { getUUID, toJson } from "@/utils/utils";

export class TrackData implements FeatureDataInterface {
    featureData: MapDataInterface;
    featureMap: Track;
    constructor({ featureData, featureMap }: {
        featureData: MapDataInterface;
        featureMap: Track;
    }) {
        this.featureData = new MapData(featureData)
        this.featureMap = new Track(featureMap)
    }
    /**
     * 移动到该要素
     * @param map
     */
    moveTo(map: AMap.Map) {
        const element = this.getDateInMap(map)
        map.setBounds(element.getExtData().bounds)
        this.blinkData(map)
    }
    /**
     * 添加要素，并返回地图要素
     * @param map
     * @returns {*}
     */
    addToMap(map: AMap.Map, mapCoordSys: MapCoordSys) {
        if (this.featureData.id === undefined) {
            this.featureData.id = getUUID();
        }
        if (this.featureMap.id === undefined) {
            this.featureMap.id = getUUID();
        } const result = this.getBaiduMapOpt(mapCoordSys, map)
        const bounds = (new AMap.Polyline(JSON.parse(JSON.stringify(result)))).getBounds()
        let feature = null;
        if (false) {
            // const overlayGroup = []
            // let lastColor = getColorBySpeed(result.speed[0].speed)
            // let lastPath = []
            // for (let i = 0; i < result.path.length - 1; i++) {
            //     let tempColor = getColorBySpeed(result.speed[i].speed)
            //     if (tempColor === lastColor) {
            //         lastPath.push(result.path[i])
            //     } else {
            //         lastPath.push(result.path[i])
            //         const overlay = new AMap.Polyline({
            //             path: lastPath,
            //             strokeColor: lastColor,
            //         })
            //         overlayGroup.push(overlay)

            //         lastColor = tempColor;
            //         lastPath = [result.path[i]]
            //     }
            // }
            // feature = new AMap.OverlayGroup(overlayGroup)
            // Object.assign(feature, {
            //     _extData: null,
            //     get extData() {
            //         return this._extData;
            //     },
            //     set extData(value) {
            //         this._extData = value;
            //     },
            //     getExtData() {
            //         return this._extData;
            //     },
            //     setExtData(value) {
            //         this._extData = value;
            //     },
            //     remove() {
            //         this.clearOverlays();
            //     }
            // });
        } else {
            feature = new AMap.Polyline(result);
        }
        feature.setExtData({
            id: this.featureData.id,
            name:this.featureData.name,
            bounds
        })
        map.add(feature)
        return feature
    }

    /**
     * 返回地图需要的数据，如果坐标不是CJ02，转换为CJ02
     * @returns {Promise<any>}
     */
    getBaiduMapOpt(mapCoordSys:MapCoordSys, map:AMap.Map) {
        const opt = JSON.parse(JSON.stringify(this.featureMap))
        //坐标转换
        switch (mapCoordSys) {
            case  MapCoordSys.CJ02:
                if (this.featureData.coordsys !== MapCoordSys.CJ02)
                    opt.path = convert_gps84_To_Gcj02(opt.path)
                break;
            case MapCoordSys.GPS:
                if (this.featureData.coordsys !== MapCoordSys.GPS)
                    opt.path = convert_gcj02_To_Gps84(opt.path)
                break;
        }
        return opt
    }


    /**
     * 更新面数据
     * @returns true:更新成功;false:更新失败
     */
    async update() {
        const result = await reqPutTrack(this)
        return true
    }
    /**
     * 添加面数据
     * @returns true:更新成功;false:更新失败
     */
    async upload() {
        const result = await reqPostTrack(this)
        return true
    }
    /**
     * 返回要素的原始设置
     * @returns {Marker}
     */
    getElement() {
        return this.featureMap
    }
    /**
     * 判断能否正确添加到该地图
     * @returns {boolean}
     */
    isElement() {
        return (this.featureMap.path as []).length > 0
    }
    /**
     * 返回复制对象
     * @returns {MarkerData}
     */
    getCopy() {
        return new TrackData(JSON.parse(JSON.stringify(this)))
    }

    /**
     * 返回地图中的对象
     * @param map
     * @returns {*}
     */
    getDateInMap(map:AMap.Map) {
        return map.getAllOverlays().find((item:any) => {
            return item.getExtData().id === this.featureData.id
        })
    }
    /**
     * 删除map中的元素
     * @param map
     */
    deleteDataInMap(map:AMap.Map) {
        map.remove(this.getDateInMap(map))
    }
    /**
     * 闪烁该要素
     * @param map
     */
    blinkData(map:AMap.Map) {
        var mapData = this.getDateInMap(map);
        //闪烁次数
        let blinkTimes = 6
        let currentTimes = 0

        const interval = setInterval(() => {
            if (currentTimes < blinkTimes) {
                if (currentTimes % 2 === 0) {
                    mapData.hide()
                } else {
                    mapData.show()
                }
                currentTimes++
            } else {
                clearInterval(interval)
                mapData.show()
            }
        }, 100)
    }
    /**
     * 下载json数据
     */
    getJSON() {
        const context = {
            mapData: this.featureData,
            feature: this.featureMap
        }
        toJson(this.featureData.name as string, context)
    }
    /**
     * 删除要素
     */
    deleteElement() {
        return reqDeleteMapData(this.featureData.id as string)
    }
     /**
     * 设置坐标点
     * @param path 坐标点
     * @returns 
     */
     setPath(path:any[]){
        this.featureMap.path=path
     }
}
export class Track implements FeatureInterface {
    id?: string
    path?: any[]
    speed?: any[]
    extData?: object
    speedShow?: boolean
    constructor({ id, path, speed, extData, speedShow }: {
        id?: string
        path?: any[]
        speed?: any[]
        extData?: object
        speedShow?: boolean
    }) {
        this.id = id
        this.path = path
        this.speed = speed
        this.extData = extData
        this.speedShow = speedShow
    }
}